let express = require('express');
let fs = require('fs');
let ejs = require('ejs');
let client = require('../util/utils.js')
let path = require('path');
let book = express.Router();
console.log(path.join(__dirname, '../view/book/book-list.html'))
//显示图书列表
book.get('/', function (request, response) {
    //读取模板文件
    fs.readFile('./view/book/book-list.html', 'utf8', function (error, data) {
         //执行SQL语句
         client.query('select * from books', function (error, results) {
              //响应信息
              response.send(ejs.render(data, {
                    data: results
              }));
         });
    });
});
// 搜索图书
book.get('/search/:keyword', function (request, response) {
    const keyword = request.params.keyword;
    console.log(request.params);
    //读取模板文件
    if (keyword ) {
     try {
          const query = 'SELECT * FROM books WHERE bookname LIKE ? OR author LIKE ? OR press LIKE ?';
          const values = [`%${keyword}%`, `%${keyword}%`, `%${keyword}%`];
          client.query(query, values,function (error, results) {
               if (error) {
                   console.error(error);
                   response.status(500).send('Internal Server Error');
                   return;
               }
               console.log(results);
               response.send(results );
          });
     } catch (error) {
          console.error(error);
          response.status(500).send('Internal Server Error');
      }
    }
});

book.get('/insert', function (request, response) {
   //读取模板文件
   fs.readFile('./view/book/book-insert.html', 'utf8', function (error, data) {
        //响应信息
        response.send(data);
   });
});
book.post('/insert', function (request, response) {
   //声明body
   let body = request.body;
   //执行SQL语句
   client.query('insert into books (bookname, author, press) VALUES (?, ?, ?)', [
        body.bookname, body.author, body.press
   ], function () {
        //响应信息
        response.redirect('/book');
   });
});


book.get('/edit/:id', function (request, response) {
   //读取模板文件
   fs.readFile('./view/book/book-edit.html', 'utf8', function (error, data) {
        //执行SQL语句
        client.query('select * from books where id = ?', [
             request.params.id
        ], function (error, result) {
             //响应信息
             response.send(ejs.render(data, {
                   data: result[0]
             }));
        });
   });
});
book.post('/edit/:id', function (request, response) {
   //声明body
   let body = request.body;
   console.log(body);

   //执行SQL语句
   client.query('update books set bookname=?, author=?, press=? where id=?',
      [body.bookname, body.author, body.press, request.params.id], function () {
        //响应信息
        response.redirect('/book');
   });
});

book.get('/delete/:id', function (request, response) {
   //执行SQL语句
   client.query('delete from books where id=?', [request.params.id], function () {
        //响应信息
        response.redirect('/book');
   });
});
module.exports = book;